/**
 * 系统后台分组管理JS-groupList.jsp
 */
//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var editstate = "";
var syseditstate = "";
$(function () {
    getList();
    //新增系统操作
    $("#btn_sysadd").click(function () {
        syseditstate = "1";
        $("#systemTitle").html("新增系统");
        $("#sysGroupName").val("");
        $("#sysRemark").val("");
        $("#addProject").modal('show');
    });

    //新增系统保存操作
    $("#btn_syssave").click(function () {
        if ($("#sysGroupName").val() == "") {
            $("#valide_sysGroupName").html("功能名称不能为空!");
            return;
        } else {
            $("#valide_sysGroupName").html("");
        }
        if (syseditstate == "1") {
            $.ajax({
                type: 'POST',
                url: postPath + '/system/group/addGroupProject',
                data: {sysGroupName: $("#sysGroupName").val(), sysRemark: $("#sysRemark").val()},
                success: function (data) {
                    if (data > 0) {
                        showMsg('新增成功!', "success");
                        getList();
                        $("#addProject").modal('hide');
                    } else {
                        showMsg('新增失败!', "error");
                    }
                }
            });
        } else if (syseditstate == "2") {
            editUrl = postPath + '/system/group/updateGroupProject';
            $.ajax({
                type: 'POST',
                url: editUrl,
                data: {
                    sysGroupId: $("#hid_sys_groupid").val(),
                    sysGroupName: $("#sysGroupName").val(),
                    sysRemark: $("#sysRemark").val()
                },
                success: function (data) {
                    if (data > 0) {
                        showMsg('修改成功!', "success");
                        getList();
                        $("#addProject").modal('hide');
                    } else {
                        showMsg('修改失败!', "error");
                    }
                }
            });
        }

    });

    //获取服务信息
    $.ajax({
        type: 'POST',
        url: postPath + '/system/group/getAllServiceInfo',
        async: false,
        success: function (data) {
            $("#serviceType").append("<option value=''>请选择</option>");
            $.each(data, function (i, e) {
                $("#serviceType").append("<option value=" + e.guid + ">" + e.servicename + "</option>");
            });
        }
    });

    //服务选择事件
    $("#serviceType").click(function () {
        getServiceName();
    });

    //功能保存操作
    $("#btn_save").click(function () {
        if ($("#groupName").val() == "") {
            $("#valide_groupName").html("功能名称不能为空!");
            return;
        } else {
            $("#valide_groupName").html("");
        }
        var temp = {
            groupid: $("#hid_groupid").val(),
            groupname: $("#groupName").val(),
            fatureid: $("#hid_fatureid").val(),
            remark: $("#Remark").val(),
            serverUrl: $("#serviceType").val(),
            serverGuid: $("#serviceId").val()
        }
        var editUrl = "";
        if (editstate == "1") {
            editUrl = postPath + '/system/group/addGroup';
        } else if (editstate == "2") {
            editUrl = postPath + '/system/group/updateGroup';
        }
        $.ajax({
            url: editUrl,
            type: 'POST',
            data: JSON.stringify(temp),
            dataType: 'json',
            contentType: "application/json",
            success: function (res) {
                if (res > 0) {
                    showMsg('保存成功!', "success");
                    getList();
                    $("#editgroup").modal('hide');
                } else {
                    showMsg('保存失败!', "error");

                }
            },
            error: function () {
                showMsg('保存失败! 请刷新后重试', "error");
            }
        });

    });
})

//默认加载数据
function getList() {
    $(".tree-2 tbody").html("");
    $.ajax({
        type: 'POST',
        url: postPath + '/system/group/getGroupList',
        async: false,
        dataType: 'json',
        contentType: "application/json",
        success: function (res) {
            var strtd = "";
            $.each(res, function (i, e) {
                if (e.fatureid == 0) {
                    strtd += "<tr class='treegrid-" + e.groupid + "'>";
                } else {
                    strtd += "<tr class='treegrid-" + e.groupid + " treegrid-parent-" + e.fatureid + "'>";
                }
                strtd += "<td>" + e.groupname + "</td><td>" + (e.serverNewUrl == null ? "" : e.serverNewUrl) + "</td><td>" + (e.serverGuid == null ? "" : e.serverGuid) + "</td><td>" + (e.remark == null ? "" : e.remark) + "</td>";
                strtd += "<td><a href='javascript:;' onclick='addClick(" + e.groupid + ",\"" + e.groupname + "\");' class='btn btn-default btn-sm'><i class='fa fa-plus'></i></a>";
                strtd += "<a href='javascript:;' onclick='updateClick(" + e.groupid + ",\"" + e.fatureid + "\",\"" + e.groupname + "\",\"" + (e.serverUrl == null ? "" : e.serverUrl) + "\",\"" + (e.serverGuid == null ? "" : e.serverGuid) + "\",\"" + (e.remark == null ? "" : e.remark) + "\");' class='btn btn-default btn-sm'><i class='fa fa-edit'></i> </a> ";
                strtd += " <a href='javascript:;' onclick='delClick(" + e.groupid + ");' class='btn btn-default btn-sm'><i class='fa fa-remove'></i> ";
                strtd += " </a></td></tr>";
            });
            $(".tree-2 tbody").append(strtd);
        }
    });
    $('.tree-2').treegrid({
        expanderExpandedClass: 'glyphicon glyphicon-minus',
        expanderCollapsedClass: 'glyphicon glyphicon-plus'
    });
}

//新增操作
function addClick(groupid, groupname) {
    cleanContent();
    editstate = "1";
    $("#fagroupName").val(groupname);
    $("#hid_fatureid").val(groupid);
    $("#editgroup").modal('show');
}

//修改操作
function updateClick(groupid, fatureid, groupname, serverUrl, serverGuid, remark) {
    cleanContent();
    if (fatureid == "0") {
        syseditstate = "2";
        $("#hid_sys_groupid").val(groupid);
        $("#sysGroupName").val(groupname);
        $("#sysRemark").val(remark);
        $("#systemTitle").html("修改系统");
        $("#addProject").modal('show');
    } else {
        editstate = "2";
        $("#group_fa").css("display", "none");
        $("#hid_groupid").val(groupid);
        $("#groupName").val(groupname);
        $("#serviceType").val(serverUrl);
        getServiceName();
        $("#serviceId").val(serverGuid);
        $("#Remark").val(remark);

        $("#editgroup").modal('show');
    }


}

//删除操作
function delClick(groupid) {
    bootbox.confirm("确认删除?", function (result) {
        if (result == false)
            return;
        $.ajax({
            url: postPath + '/system/group/delGroup',
            type: 'POST',
            data: {groupId: groupid},
            success: function (res) {
                if (res > 0) {
                    showMsg('删除成功!', "success");
                    getList();
                } else {
                    showMsg('删除失败!', "error");
                }
            },
            error: function () {
                showMsg('删除失败!', "error");
            }
        });
    });
}

//清空信息
function cleanContent() {
    $("#fagroupName").val("");
    $("#hid_fatureid").val("");
    $("#groupName").val("");
    $("#serviceType").val("");
    $("#serviceId").val("");
    $("#Remark").val("");
    $("#group_fa").css("display", "block");
}

//服务名称
function getServiceName() {
    $("#serviceId").html("");
    $.ajax({
        type: 'POST',
        url: postPath + '/system/group/getServiceInfoByguid',
        data: {guid: $("#serviceType").val()},
        async: false,
        success: function (data) {
            $("#serviceId").append("<option value=''>请选择</option>");
            $.each(data, function (i, e) {
                $("#serviceId").append("<option value=" + e.key + ">" + e.value + "</option>");
            });
        }
    });
}

function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600',
    });
}

